//
//  ViewController.m
//  10-TableViewCellHomework
//
//  Created by 曹曹 on 16/7/27.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "GXTableViewHeadView.h"
#import "UIView+Frame.h"
#import "CarsTableViewCell.h"
#import "Model.h"

#define kTableViewNormalIdentifier @"reuse_normal"
#define kTableViewImageIdentifier @"reuse_image"

@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *arrOfModel;
@property (nonatomic, strong) NSMutableArray *arrOfCycleImage;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    GXTableViewHeadView *headView = [[GXTableViewHeadView alloc] initWithFrame:CGRectMake(0, 0, self.view.gx_width, self.view.gx_height / 3 + 50) images:_arrOfCycleImage];
    self.tableView.tableHeaderView = headView;
    [self.tableView registerClass:[CarsTableViewCell class] forCellReuseIdentifier:kTableViewNormalIdentifier];
}
#pragma mark - handle data
- (void)handleData {
    NSString *path = [[NSBundle mainBundle]pathForResource:@"YiChe" ofType:@"plist"];
    NSDictionary *dictionOfSource = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *arraySource = [[dictionOfSource objectForKey:@"data"] objectForKey:@"list"];
    NSArray *arrayCycle = [[dictionOfSource objectForKey:@"data"] objectForKey:@"cycleImage"];
#warning arrOfCycleImage
    self.arrOfCycleImage = [NSMutableArray array];
    for (NSDictionary *cycleDiction in arrayCycle) {
        NSString *picurl = [cycleDiction objectForKey:@"picCover"];
        [_arrOfCycleImage addObject:picurl];
    }
#warning arrOfModel
    self.arrOfModel = [NSMutableArray array];
    for (NSDictionary *diction in arraySource) {
        Model *model = [[Model alloc] init];
        model.title = [diction objectForKey:@"title"];
        model.picCover = [diction objectForKey:@"picCover"];
        [_arrOfModel addObject:model];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
#pragma mark - data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrOfModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CarsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewNormalIdentifier];
    Model *model = [_arrOfModel objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
